package messageJSON

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"system-admin/internal/models"
)

/*
博客处理消息集
*/

// 204:获取博客文章N条和相关用户数据成功
func GetBlogEssayAndUserOK(c *gin.Context, blogList *models.BlogList, userList *models.UserList, row int) (message string) {
	code := "200"
	message = fmt.Sprintf("获取博客文章N条和相关用户数据成功，返回了%d条数据", row)
	log.Println(code + ": " + message)

	////将blogList结构体转成byte[]再转成string装入请求中
	//blogListJSON, err := json.Marshal(&blogList)
	//blogListJSONString := string(blogListJSON)
	//
	////将userList结构体转成byte[]再转成string装入请求中
	//userListJSON, err := json.Marshal(&userList)
	//userListJSONString := string(userListJSON)

	blogAndUserList := &models.BlogAndUserList{
		BlogEssay: blogList.BlogEssay,
		User:      userList.User,
	}

	blogAndUserListJSON, err := json.Marshal(&blogAndUserList)
	blogAndUserListJSONString := string(blogAndUserListJSON)

	if err != nil {
		log.Println(err)
	}

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    blogAndUserListJSONString,
	})
	return
}

// 404:获取博客文章N条和相关用户数据失败
func GetBlogEssayAndUserFail(c *gin.Context) (message string) {
	code := "404"
	message = fmt.Sprintf("获取博客文章N条和相关用户数据失败")
	log.Println(code + ": " + message)

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    "",
	})
	return
}
